import Vue from 'vue'
import Router from 'vue-router'
import { constantRouterMap } from './router.config.js'
import Cookies from 'js-cookie'

const originalPush = Router.prototype.push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router)

const createRouter = () =>
  new Router({
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRouterMap
  })

const router = createRouter()
router.beforeEach((to, from, next) => {
  if (to.matched.some(r => r.meta.requireAuth)) {
    meta:{requireAuth:true}
    if (Cookies.get('Token')) {
        next();
    } else {
        next({
            path: '/login',
            query: {redirect: to.fullPath}
        })
    }
  } else {
    next();
  }
})

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

export default router
